package com.wenhui.nzapp.ui.activity.dinnerqr;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSONObject;
import com.gzwh.weblib.bean.NzDinnerRegion;
import com.gzwh.weblib.bean.Result;
import com.gzwh.weblib.utils.MyObserver;
import com.gzwh.weblib.utils.RequestUtils;
import com.wenhui.nzapp.R;
import com.wenhuitec.sysui.TitleBuilder;

public class DinnerQrAreEditActivity extends AppCompatActivity {

    private TextView nameEd;
    private Context mContext;

    private String ardId;
    private boolean updateOrSave = false;

    private LinearLayout deleteAre;


    public static void startAct(Context context, NzDinnerRegion nzDinnerRegion) {
        Intent intent = new Intent(context, DinnerQrAreEditActivity.class);
        intent.putExtra("data", JSONObject.toJSONString(nzDinnerRegion));
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dinnerqr_areedit);

        nameEd = findViewById(R.id.nameEd);
        deleteAre = findViewById(R.id.deleteAre);
        mContext = this;
        try {
            String data = this.getIntent().getExtras().getString("data");
            NzDinnerRegion nzDinnerRegion = JSONObject.parseObject(data, NzDinnerRegion.class);
            updateOrSave = true;
            nameEd.setText(nzDinnerRegion.getName());
            ardId = nzDinnerRegion.getId();
            deleteAre.setVisibility(View.VISIBLE);
        } catch (Exception e) {
//            e.printStackTrace();
        }

        new TitleBuilder(this)
                .setTitleText(updateOrSave ? "编辑区域" : "新增区域")
//                .setLeftIco(R.drawable.yp)
                .setLeftIcoListening((view) -> finish());

        findViewById(R.id.saveCateLinear).setOnClickListener(view -> {
            if (updateOrSave) {
                RequestUtils.updateAre(mContext, ardId, nameEd.getText().toString(), new MyObserver<String>(mContext) {
                    @Override
                    public void onSuccess(Result<String> result) {
                        Toast.makeText(mContext, "操作成功", Toast.LENGTH_SHORT).show();
                        finish();
                    }

                    @Override
                    public void onFailure(Throwable e, String errorMsg) {
                        Toast.makeText(mContext, errorMsg, Toast.LENGTH_SHORT).show();

                    }
                });
            } else {
                RequestUtils.addAre(mContext, nameEd.getText().toString(), new MyObserver<String>(mContext) {
                    @Override
                    public void onSuccess(Result<String> result) {
                        Toast.makeText(mContext, "操作成功", Toast.LENGTH_SHORT).show();
                        finish();
                    }

                    @Override
                    public void onFailure(Throwable e, String errorMsg) {
                        Toast.makeText(mContext, errorMsg, Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
        findViewById(R.id.deleteBut).setOnClickListener(view -> {
            RequestUtils.deleteAre(mContext, ardId, new MyObserver<String>(mContext) {
                @Override
                public void onSuccess(Result<String> result) {
                    Toast.makeText(mContext, "操作成功", Toast.LENGTH_SHORT).show();
                    finish();
                }

                @Override
                public void onFailure(Throwable e, String errorMsg) {
                    Toast.makeText(mContext, errorMsg, Toast.LENGTH_SHORT).show();

                }
            });
        });
    }
}
